package com.im.oauth2.config;

import com.im.oauth2.config.jwt.JwtTokenEnhancer;
import com.im.oauth2.propertiy.Oauth2Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * @author gongym
 * @date 2018年11月13日 上午9:19:52
 */
@Configuration
public class TokenStoreConfig {

  /** jwt TOKEN配置信息 */
  @Configuration
  public static class JwtTokenConfig {
    private Oauth2Properties oAuth2Properties;

    @Autowired
    public JwtTokenConfig(Oauth2Properties oAuth2Properties) {
      this.oAuth2Properties = oAuth2Properties;
    }
    /** 使用jwtTokenStore存储token */
    @Bean
    public TokenStore jwtTokenStore() {
      return new JwtTokenStore(jwtAccessTokenConverter());
    }
    /** 用于生成jwt */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
      JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
      accessTokenConverter.setSigningKey(oAuth2Properties.getJwtSigningKey());
      return accessTokenConverter;
    }
    /** 用于扩展JWT */
    @Bean
    @ConditionalOnMissingBean(name = "jwtTokenEnhancer")
    public TokenEnhancer jwtTokenEnhancer() {
      return new JwtTokenEnhancer();
    }
  }
}
